#include "StdAfx.h"
#include "BitString.h"

BitString::BitString(int lenght)
{
	this->_bitstr = new char[((lenght % BYTEBITS) == 0) ? (lenght/BYTEBITS) : (lenght/BYTEBITS +1)];
	this->_lenght = (byte)lenght;

	//initialize random bytes
	srand((unsigned)time(NULL));

	for (int i=0; i < this->_lenght; i++)
		this->_bitstr[i] = (byte)(rand() ^ 33);
}

BitString::~BitString(void)
{
	delete[] this->_bitstr;
}

bool BitString::getBit(int n)
{
	return (bool)( (this->_bitstr[n/BYTEBITS] & (1 << (n % BYTEBITS))) != 0 );
}

void BitString::setBit(int n, bool b)
{
	if(b)
		this->_bitstr[n/BYTEBITS] |= 1 << ( n % BYTEBITS ); //true
	else
		this->_bitstr[n/BYTEBITS] &= ~(1 << ( n % BYTEBITS )); //false
}

void BitString::flipBit(int n)
{
	this->_bitstr[n/BYTEBITS] ^= (1 << (n % BYTEBITS));
}

ulong BitString::toUlong()
{
	return 0;
}